﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Twipler.Data.Entity.Twipler;
using Twipler.WebApp;
using Twipler.App.Parse.Html.Consumers;
using Twipler.App.Parse.Html;
using System.Reflection;

namespace Settings.Sandbox
{
    public partial class Instructions : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder builder = new StringBuilder();

            builder.Append("<h2>Render tags</h2>");

            foreach (KeyValuePair<string, Type> consumer in Twipler.App.Parse.Html.ConsumerFactory.Consumers)
            {
                ConsumerAttribute[] attrib = (ConsumerAttribute[])consumer.Value.GetCustomAttributes(typeof(ConsumerAttribute), true);
                if (attrib.Count() != 1)
                    continue;

                builder.AppendFormat("Tag: {0} ", attrib[0].OpenTag);

                if (!string.IsNullOrEmpty(attrib[0].CloseTag))
                    builder.AppendFormat("{0}", attrib[0].CloseTag);

                builder.AppendFormat("<br/>");

                if ((attrib[0].OptionalTags != null) && (attrib[0].OptionalTags.Count() > 0))
                {
                    builder.Append("- optional: ");
                    foreach (string s in attrib[0].OptionalTags)
                        builder.Append(s);

                    builder.Append("<br/>");
                }


                if (attrib[0].Parameters == default(Type))
                    builder.Append("- no parameters<br/>");
                else
                {
                    
                    foreach (FieldInfo fi in attrib[0].Parameters.GetFields())
                    {
                        HtmlArgumentAttribute[] attributes =
                            (HtmlArgumentAttribute[])fi.GetCustomAttributes(typeof(HtmlArgumentAttribute), false);

                        if (attributes.Count() != 1)
                            continue;

                        builder.AppendFormat("- param: {0} type:{1} optional: {2}<br/>", fi.Name, attributes[0].TokenType, attributes[0].Optional);
                    }
                }

                builder.Append("<br/>");
            }

            builder.Append("<br/><br/><br/>");

            builder.AppendFormat("<h3>Methods</h3>");


            foreach (MethodInfo mi in DataDocTest.GetCallableMethods())
            {
                builder.AppendFormat("{0}", mi.DeclaringType.Name + "." + mi.Name + "( "); // + mi.ToString().Substring(mi.ToString().IndexOf(' ') + 1));

                for (int i = 0; i < mi.GetParameters().Length; i++)
                    builder.AppendFormat("{0} {1}{2}", mi.GetParameters()[i].ParameterType.ToString().Replace("System.", "").ToLower(), 
                        mi.GetParameters()[i].Name,
                        i < mi.GetParameters().Length - 1 ? ", " : " ");

                builder.AppendFormat(" )<br/>");
            }

            
            builder.Append("<br/><br/><br/>");


            // -------------

            builder.Append("<h2>Program instructions</h2>");
            builder.Append("<table class=\"grid\" cellspacing=\"0\"><tr class=\"gridhead\"><td>OpCode</td><td>Pop</td><td>Push</td><td>Description</td></tr>");

            foreach (Type t in Twipler.App.Parse.Code.Instructions._types)
            {

                builder.AppendFormat("<tr><td>{0}</td>", t.Name);

                Twipler.App.Machine.InstructionAttibute instAtt = null;
                foreach (object o in t.GetCustomAttributes(false))
                    if (o is Twipler.App.Machine.InstructionAttibute)
                        instAtt = (Twipler.App.Machine.InstructionAttibute)o;

                if (instAtt == null)
                    builder.Append("<td colspan=\"3\"></td>");
                else
                    builder.AppendFormat("<td>{0}</td><td>{1}</td><td>{2}</td>", instAtt.pops, instAtt.pushes, instAtt.description);

            }

            litInstructions.Text = builder.ToString();

        }
    }
}